﻿//题目列表操作
//获取题目列表
function BindGaugeTestList(GaugeId) {
    $.ajax({
        type: "post",
        datatype: "json",
        url: "GaugeEdit.aspx/GetGaugeTestList",
        contentType: "application/json",
        data: "{GaugeId:" + GaugeId + "}",
        success: function (result) {
            var $grid_GaugeTest = $("#grid_GaugeTest");
            $grid_GaugeTest.find("tr:not(:first)").remove();
            var data = result.d;
            for (var i = 0; i < data.length; i++) {
                var clickEven = "";
                if (data[i].TestType == "STEP_GRID") {
                    clickEven = 'showTestItemWindows2(' + data[i].GaugeTestId + ')';
                } else {
                    clickEven = 'showTestItemWindows(' + data[i].GaugeTestId + ')';
                }
                var _html = '<td>' + data[i].GaugeTestId + '</td>'
                            + '<td>' + data[i].TestType + '</td>'
                            + '<td>' + data[i].Title + '</td>'
                            + '<td style="text-align: center;">启用</td>'
                            + '<td><a href="javascript:;" onclick="' + clickEven + '">编辑</a>'
                            + '&nbsp;&nbsp;<a href="javascript:;" onclick="delGaugeTestData(' + data[i].GaugeTestId + ',this)">删除</a></td>';
                _html = '<tr>' + _html + '</tr>';
                $grid_GaugeTest.append(_html);
            }
        }
    });
}
//题目操作
//保存题目
function SaveTestData() {

    var GaugeTestId = $('#hf_GaugeTestId').val();
    $('#txt_TestTitle').val(editor.html());
    var TestTitle = $('#txt_TestTitle').val();
    var TestType = $('#ddl_TestType').val();
    var model = {
        GaugeId: GaugeId,
        GaugeTestId: GaugeTestId,
        Title: TestTitle,
        TestType: TestType
    };
    var list = new Array();
    var ItemTypeNo = $('#ddl_TestType').val();
    if (ItemTypeNo == "RADIO" || ItemTypeNo == "CHECKBOX") {
        $('#grid_radio tr:not(:first)').each(function () {
            var inputs = $(this).find('input');
            var item_model = new Object();
            item_model.GaugeTestItemId = $(inputs[0]).val();
            item_model.Value = $(inputs[1]).val();
            item_model.Score = $(inputs[2]).val();
            item_model.ItemTypeNo = ItemTypeNo + ($(inputs[3]).attr("checked") == "checked" ? "_TEXT" : "");
            list[list.length] = item_model;
        });
    } else if (ItemTypeNo == "AREATEXT") {
        $('#grid_radio tr:not(:first)').first().each(function () {
            var inputs = $(this).find('input');
            var item_model = new Object();
            item_model.GaugeTestItemId = $(inputs[0]).val();
            item_model.Value = "";
            item_model.Score = 0;
            item_model.ItemTypeNo = "AREATEXT";
            list[list.length] = item_model;
        });
    }
    var _data = { JsonModel: JSON.stringify(model), JsonList: JSON.stringify(list) };
    $.ajax({
        type: "post",
        datatype: "json",
        url: "GaugeEdit.aspx/SaveGaugeTestDate",
        contentType: "application/json",
        data: JSON.stringify(_data),
        success: function (result) {
            if (result.d > 0) {
                alert("保存操作成功！");
                getGaugeTestData(result.d);
                BindGaugeTestList(GaugeId);
            }
            else {
                alert("操作失败！");
            }
        }
    });
}

//获取单个题目详细内容
function getGaugeTestData(id) {
    $.ajax({
        type: "post",
        datatype: "json",
        url: "GaugeEdit.aspx/GetGaugeTestData",
        contentType: "application/json",
        data: "{Id:" + id + "}",
        success: function (result) {
            var data = result.d;
            if (data != null) {
                $('#txt_TestTitle').val(data.Title);
                editor.html($('#txt_TestTitle').val());
                $('#ddl_TestType').val(data.TestType);
                $('#hf_GaugeTestId').val(data.GaugeTestId);

                ddl_TestTypeChange();

                getGaugeTestItemList(data.GaugeTestId);
            }
        }
    });
}
//获取题目的选择列表
function getGaugeTestItemList(gaugeTestId) {
    $('#grid_radio tr:not(:first)').remove();
    $.ajax({
        type: "post",
        datatype: "json",
        url: "GaugeEdit.aspx/GetGaugeTestItemList",
        contentType: "application/json",
        data: "{GaugeTestId:" + GaugeTestId + "}",
        success: function (result) {
            var data = result.d;
            for (var i = 0; i < data.length; i++) {
                addRadioItem(data[i].GaugeTestItemId, data[i].Value, data[i].Score, (data[i].ItemTypeNo == "RADIO_TEXT" || data[i].ItemTypeNo == "CHECKBOX_TEXT"));
            }
        }
    });
}
//删除题目的选择列表
function delGaugeTestData(gaugeTestId, obj) {
    if (!confirm('确定要删除该数据吗？')) {
        return;
    }
    $.ajax({
        type: "post",
        datatype: "json",
        url: "GaugeEdit.aspx/DelGaugeTestData",
        contentType: "application/json",
        data: "{GaugeTestId:" + gaugeTestId + "}",
        success: function (result) {
            var data = result.d;
            if (data > 0) {
                delRadioItem(obj);
            }
            else {
                alert('操作失败！');
            }
        }
    });
}

function addRadioItem(id, value, Score, isShowText) {
    $("#grid_radio").append('<tr><td style="display:none">' + id + '<input type="hidden" value="' + id + '"/></td>'
                            + '<td><input type="text" class="ht-text" style="width: 360px" value="' + value + '"/></td>'
                            + '<td><input type="text" class="ht-text" value="' + Score + '" style="width: 40px" /></td>'
                            + '<td style="text-align:center"><input type="checkbox" name="isText" ' + (isShowText == true ? 'checked="checked"' : '') + '/></td>'
                            + '<td><a href="javascript:;" onclick="delRadioItem(this)">删除</a></td></tr>');
}

function delRadioItem(obj) {
    $(obj).parent("td").parent("tr").remove();
}

function SaveRadioItem() {
    var list = new Array();
    var ItemTypeNo = $('#ddl_TestType').val();
    $('#grid_radio tr:not(:first)').each(function () {
        var inputs = $(this).find('input');
        var model = new Object();
        model.GaugeTestItemId = $(inputs[0]).val();
        model.Value = $(inputs[1]).val();
        model.Score = $(inputs[2]).val();
        model.ItemTypeNo = ItemTypeNo + ($(inputs[3]).attr("checked") == "checked" ? "_TEXT" : "");
        list[list.length] = model;
    });
    $.ajax({
        type: "post",
        datatype: "json",
        url: "GaugeEdit.aspx/SaveGaugeTestItemList",
        contentType: "application/json",
        data: "{TestId:1, List:'" + JSON.stringify(list) + "'}",
        success: function (result) {
            if (result.d == "OK") {
                alert("保存操作成功！");
                getGaugeTestItemList(1);
            }
            else {
                alert(result.d);
            }
        }
    });
}

//下拉框，改变事件
function ddl_TestTypeChange() {
    $('#grid_radio tr:not(:first)').remove();

    var testtype = $('#ddl_TestType').val();
    if (testtype == "AREATEXT") {
        $('#div_Radio').hide();
        addRadioItem(-1, "", "0", false);
    }
    else if (testtype == "TEXT") {
        $('#div_Radio').show();
    }
    else {
        $('#div_Radio').show();
        for (var i = 0; i < 4; i++) {
            addRadioItem(-1, "", "0", false);
        }
    }
}

//显示弹出窗
function showTestItemWindows(id) {
    $('#winTestItem').show();
    $('#txt_TestTitle').val('');
    editor.html('');
    $('#hf_GaugeTestId').val('-1');
    $('#ddl_TestType').val('RADIO');
    $('#grid_radio tr:not(:first)').remove();

    $('#winTestItem').window('open', { title: '新增' });

    if (id > 0) {
        $('#ddl_TestType').attr('disabled', 'disabled');
        getGaugeTestData(id);
    }
    else {
        $('#ddl_TestType').removeAttr('disabled');
        for (var i = 0; i < 4; i++) {
            addRadioItem(-1, "", "0", false);
        }
    }
}

